//
//  LPSingleComponentPickerViewController.m
//  Pickers
//
//  Created by johnny tang on 7/25/13.
//  Copyright (c) 2013 TOSC. All rights reserved.
//

#import "LPSingleComponentPickerViewController.h"

@interface LPSingleComponentPickerViewController ()

@end

@implementation LPSingleComponentPickerViewController

@synthesize singlePicker;
@synthesize button;
@synthesize pickerData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia",
                      @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
    self.pickerData = array;
    
    UILongPressGestureRecognizer *longPressGesutreRecognizer = [[UILongPressGestureRecognizer alloc]
                                                                initWithTarget:self action:@selector(handleLongPressed:)];
    longPressGesutreRecognizer.minimumPressDuration = 0.8;
    [button addGestureRecognizer:longPressGesutreRecognizer];
    longPressGesutreRecognizer = nil;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void) viewDidUnload {
    [super viewDidUnload];
    self.singlePicker = nil;
    self.pickerData = nil;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonPressed:(id)sender {
    NSInteger row = [singlePicker selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    NSString *title = [[NSString alloc] initWithFormat:@"Your seleted at %@!",selected] ;
    UIAlertView *alter = [[UIAlertView alloc]
                          initWithTitle:title
                          message:@"Thank you for choosing"
                          delegate:nil
                          cancelButtonTitle:@"Your are welcom"
                          otherButtonTitles:nil];
    [alter show];
}

-(void) handleLongPressed: (UILongPressGestureRecognizer *) gestureRecognizer{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        NSLog(@"skdfjsladjglajg!");
        UIAlertView *alter = [[UIAlertView alloc]
                              initWithTitle:@"button long pressed"
                              message:@"Thank you for choosing"
                              delegate:nil
                              cancelButtonTitle:@"Your are welcom"
                              otherButtonTitles:nil];
        [alter show];
    }
    
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [pickerData count];
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    
    return [pickerData objectAtIndex:row];
}

@end
